import { _HttpClient } from '@delon/theme';
import { Injectable } from '@angular/core';
import { QueryOptions } from 'src/app/project/model/common/query-options';
import { Observable } from 'rxjs';
import { ApiPagedData } from 'src/app/project/model/common/api-paged-data';
import { SystemDictionaryDTO } from 'src/app/project/model/icm/icm-global/dictionary/dictionary.dto';
import { ApiSimpleData } from 'src/app/project/model/common/api-simple-data';
import { SystemDictionaryValueDTO } from 'src/app/project/model/icm/icm-global/dictionary/dictionary-value.dto';

@Injectable({
  providedIn: 'root',
})
export class DictionaryService {
  /**
   * API的入口URL
   */
  private static URL = '/api/icm-dictionary';

  constructor(private http: _HttpClient) {}

  /**
   * 分页查询字典
   * @param option 分页参数
   * @param code 字典编号
   * @param name 字典名称
   */
  findOnePage(option: QueryOptions, code?: string, name?: string): Observable<ApiPagedData<SystemDictionaryDTO>> {
    const params = {};
    Object.assign(params, option, code ? { code } : {}, name ? { name } : {});
    return this.http.get<ApiPagedData<SystemDictionaryDTO>>(DictionaryService.URL, params);
  }

  /**
   * 新增字典
   */
  create(dictionaryInfo: SystemDictionaryDTO): Observable<SystemDictionaryDTO> {
    return this.http.post<SystemDictionaryDTO>(DictionaryService.URL, dictionaryInfo);
  }

  /**
   * 提交字典修改数据
   * @param id 字典Id
   * @param editInfo 字典信息
   */
  complete(id: string, editInfo: SystemDictionaryDTO): Observable<SystemDictionaryDTO> {
    return this.http.put<SystemDictionaryDTO>(`${DictionaryService.URL}/${id}`, editInfo);
  }

  /**
   * 删除字典
   * @param id 字典id
   */
  delete(id: string): Observable<any> {
    return this.http.delete(`${DictionaryService.URL}/${id}`);
  }

  /**
   * 查找所有字典
   */
  findAll(): Observable<Array<SystemDictionaryDTO>> {
    return this.http.get<Array<SystemDictionaryDTO>>(`${DictionaryService.URL}/all`);
  }

  /**
   * 根据字典编号查找字典值，字典值用于下拉数据
   * @param code 字典编号
   * @param all 是否为全部值（包括available为false的值)
   */
  findValueByCode(code: string, all?: boolean): Observable<SystemDictionaryDTO> {
    const params = {};
    Object.assign(params, all ? { all } : {});
    return this.http.get<SystemDictionaryDTO>(`${DictionaryService.URL}/code/${code}`, params);
  }

  /**
   * 根据字典id，检查字典编号code是否已存在
   * @param dictionaryId 字典id
   * @param code 字典编号
   */
  checkoutCodeIsExist(dictionaryId: string, code: string): Observable<ApiSimpleData<boolean>> {
    const params = {};
    Object.assign(params, code ? { code } : {});
    return this.http.get<ApiSimpleData<boolean>>(`${DictionaryService.URL}/${dictionaryId}/code`, params);
  }

  /**
   * 新建字典值
   * @param dictionaryId 字典id
   * @param dictionaryValue 字典值
   */
  createValue(dictionaryId: string, dictionaryValue: SystemDictionaryValueDTO): Observable<SystemDictionaryValueDTO> {
    return this.http.post<SystemDictionaryValueDTO>(`${DictionaryService.URL}/${dictionaryId}/values`, dictionaryValue);
  }

  /**
   * 修改字典值
   * @param dictionaryId 字典id
   * @param valueId 字典值id
   * @param dictionaryValue 字典值
   */
  completeValue(
    dictionaryId: string,
    valueId: string,
    dictionaryValue: SystemDictionaryValueDTO,
  ): Observable<SystemDictionaryValueDTO> {
    return this.http.put<SystemDictionaryValueDTO>(
      `${DictionaryService.URL}/${dictionaryId}/values/${valueId}`,
      dictionaryValue,
    );
  }

  /**
   * 删除字典值
   * @param dictionaryId 字典id
   * @param valueId 字典值id
   */
  deleteValue(dictionaryId: string, valueId: string): Observable<any> {
    return this.http.delete(`${DictionaryService.URL}/${dictionaryId}/values/${valueId}`);
  }
  /**
   * 根据字典id和字典值id，检查字典值的键是否已存在
   * @param dictionaryId 字典id
   * @param dictionaryValueId 字典值id
   * @param key 字典值的键
   */
  checkoutKeyIsExist(dictionaryId: string, dictionaryValueId: string, key: string): Observable<ApiSimpleData<boolean>> {
    const params = {};
    Object.assign(params, dictionaryValueId ? { dictionaryValueId } : {}, key ? { key } : {});
    return this.http.get<ApiSimpleData<boolean>>(`${DictionaryService.URL}/${dictionaryId}/values/key`, params);
  }
}
